#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX  41
int count;
int main(void)
{
	count =0;
	FILE *fp;
	char words[MAX];
	
	if((fp = fopen("wordy", "a+")) == NULL)
	{
		fprintf(stdout, "can't open \"wordy\"file.\n");
		exit(EXIT_FAILURE);
	}
	while (fgets(words, MAX, fp) != NULL)
    {
        count++;
    }	
	puts("Enter words to add to the file");
	while(fscanf(stdin, "%40s", words) == 1
								 && (words[0] != '#'))
	{
		fprintf(fp, "%d): %s\n",count, words);
		count++;		
	}			
	puts("File contents:");
	rewind(fp);
	while(fgets(words, MAX, fp) != NULL)
	{
		fputs(words, stdout);
	}
	puts("DONE!");
	if(fclose(fp) != 0)
		fprintf(stderr, "Error closing file\n");
	return 0;
}
